<template>
  <div>
    <a-layout>
      <a-layout-content style="height: calc(100vh - 88px); padding: 2px">
        <VxeBasicTable ref="TableRef" v-bind="options">
          <template #toolbar_buttons>
            <ToolBtn
              :filter-call="handleOpenFilter"
              :create-call="handleEdit"
              :delete-call="handleDelete"
              :show-export="false"
              :show-import="false"
              :show-delete="false"
            />
          </template>
          <template #action="{ row }">
            <Action :record="row" @edit="handleEdit" @delete="handleDelete" />
          </template>
        </VxeBasicTable>
      </a-layout-content>
    </a-layout>
    <DataResourceModal
      v-model:visible="showEdit"
      :data="current"
      :orglist="treeData"
      @done="reload"
    />
  </div>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { message } from 'ant-design-vue/es';
  import { VxeBasicTable } from '@/components/VxeTable';
  import { messageLoading } from 'ele-admin-pro/es';
  import { list, remove } from '@/api/system/dataResource';
  import DataResourceModal from './components/DataResourceModal.vue';
  import { useConfig } from './config';
  import type { SysDataResource } from '@/api/system/dataResource/model';

  const { TableRef, CurrentData, ToolBtn, Action, options } = useConfig(list);
  const ifSHowFilter = ref(false);

  const treeData = ref<any>([]);

  // 当前编辑数据
  const current = ref<SysDataResource | null>(null);

  // 是否显示编辑弹窗
  const showEdit = ref(false);

  /* 刷新表格 */
  const reload = () => {
    TableRef.value?.commitProxy('query');
  };

  /* 打开编辑弹窗 */
  const handleEdit = (row?: SysDataResource | null) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  const handleOpenFilter = () => {
    ifSHowFilter.value = true;
  };

  /* 删除单个 */
  const handleDelete = (row: SysDataResource) => {
    const hide = messageLoading('请求中..', 0);
    remove(row.id)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };
  onMounted(() => {
    treeData.value = CurrentData;
  });
</script>

<script lang="ts">
  export default {
    name: 'SystemDataResource'
  };
</script>
